// 小工具功能
function initToolsModule() {
  // 万年历功能
  initCalendarTool();
  
  // 星座生肖功能
  initZodiacTool();
  
  // 姓名测算功能
  initNameAnalysisTool();
  
  // 周公解梦功能
  initDreamTool();
}

// 万年历功能
function initCalendarTool() {
  const calendarForm = document.getElementById('calendarForm');
  if (!calendarForm) return;
  
  calendarForm.addEventListener('submit', function(e) {
    e.preventDefault();
    
    const year = document.getElementById('calendarYear').value;
    const month = document.getElementById('calendarMonth').value;
    
    // 获取万年历数据
    toolAPI.getCalendar(year, month)
      .then(response => {
        if (response.code === 200) {
          showCalendarResult(response.data);
        } else {
          showToast(response.message || '获取万年历数据失败');
        }
      })
      .catch(error => {
        showToast(error.message || '获取万年历数据失败');
      });
  });
}

// 显示万年历结果
function showCalendarResult(data) {
  const resultContainer = document.getElementById('calendarResultContainer');
  if (!resultContainer) return;
  
  resultContainer.style.display = 'block';
  
  const calendarContent = document.getElementById('calendarContent');
  if (calendarContent) {
    let html = `
      <h3 class="text-xl font-bold mb-4">${data.year}年${data.month}月</h3>
      <div class="grid grid-cols-7 gap-1">
        <div class="text-center font-medium p-2 bg-indigo-100">日</div>
        <div class="text-center font-medium p-2 bg-indigo-100">一</div>
        <div class="text-center font-medium p-2 bg-indigo-100">二</div>
        <div class="text-center font-medium p-2 bg-indigo-100">三</div>
        <div class="text-center font-medium p-2 bg-indigo-100">四</div>
        <div class="text-center font-medium p-2 bg-indigo-100">五</div>
        <div class="text-center font-medium p-2 bg-indigo-100">六</div>
    `;
    
    // 添加空白格子
    for (let i = 0; i < data.firstDayOfWeek; i++) {
      html += `<div class="p-2 border"></div>`;
    }
    
    // 添加日期格子
    for (let i = 1; i <= data.daysInMonth; i++) {
      const dayInfo = data.days.find(day => day.day === i);
      const isToday = dayInfo && dayInfo.isToday;
      const lunarDay = dayInfo ? dayInfo.lunarDay : '';
      const solarTerm = dayInfo && dayInfo.solarTerm ? `<span class="text-green-600">${dayInfo.solarTerm}</span>` : '';
      const festival = dayInfo && dayInfo.festival ? `<span class="text-red-600">${dayInfo.festival}</span>` : '';
      
      html += `
        <div class="p-2 border ${isToday ? 'bg-indigo-100' : ''}">
          <div class="text-center font-medium">${i}</div>
          <div class="text-center text-xs text-gray-600">${lunarDay}</div>
          <div class="text-center text-xs">${solarTerm}</div>
          <div class="text-center text-xs">${festival}</div>
        </div>
      `;
    }
    
    html += `</div>`;
    calendarContent.innerHTML = html;
  }
}

// 星座生肖功能
function initZodiacTool() {
  const zodiacSignForm = document.getElementById('zodiacSignForm');
  if (zodiacSignForm) {
    zodiacSignForm.addEventListener('submit', function(e) {
      e.preventDefault();
      
      const month = document.getElementById('zodiacMonth').value;
      const day = document.getElementById('zodiacDay').value;
      
      // 获取星座信息
      toolAPI.getZodiacSign(month, day)
        .then(response => {
          if (response.code === 200) {
            showZodiacSignResult(response.data);
          } else {
            showToast(response.message || '获取星座信息失败');
          }
        })
        .catch(error => {
          showToast(error.message || '获取星座信息失败');
        });
    });
  }
  
  const chineseZodiacForm = document.getElementById('chineseZodiacForm');
  if (chineseZodiacForm) {
    chineseZodiacForm.addEventListener('submit', function(e) {
      e.preventDefault();
      
      const year = document.getElementById('zodiacYear').value;
      
      // 获取生肖信息
      toolAPI.getChineseZodiac(year)
        .then(response => {
          if (response.code === 200) {
            showChineseZodiacResult(response.data);
          } else {
            showToast(response.message || '获取生肖信息失败');
          }
        })
        .catch(error => {
          showToast(error.message || '获取生肖信息失败');
        });
    });
  }
}

// 显示星座结果
function showZodiacSignResult(data) {
  const resultContainer = document.getElementById('zodiacSignResultContainer');
  if (!resultContainer) return;
  
  resultContainer.style.display = 'block';
  
  const zodiacSignContent = document.getElementById('zodiacSignContent');
  if (zodiacSignContent) {
    zodiacSignContent.innerHTML = `
      <div class="flex items-center mb-4">
        <img src="${data.image}" alt="${data.name}" class="w-16 h-16 mr-4">
        <div>
          <h3 class="text-xl font-bold">${data.name}</h3>
          <p class="text-gray-600">${data.date}</p>
        </div>
      </div>
      <div class="space-y-3">
        <div>
          <h4 class="font-medium">性格特点</h4>
          <p>${data.personality}</p>
        </div>
        <div>
          <h4 class="font-medium">优点</h4>
          <p>${data.strengths}</p>
        </div>
        <div>
          <h4 class="font-medium">缺点</h4>
          <p>${data.weaknesses}</p>
        </div>
        <div>
          <h4 class="font-medium">爱情观</h4>
          <p>${data.loveView}</p>
        </div>
        <div>
          <h4 class="font-medium">事业发展</h4>
          <p>${data.career}</p>
        </div>
        <div>
          <h4 class="font-medium">幸运色</h4>
          <p>${data.luckyColor}</p>
        </div>
        <div>
          <h4 class="font-medium">幸运数字</h4>
          <p>${data.luckyNumber}</p>
        </div>
      </div>
    `;
  }
}

// 显示生肖结果
function showChineseZodiacResult(data) {
  const resultContainer = document.getElementById('chineseZodiacResultContainer');
  if (!resultContainer) return;
  
  resultContainer.style.display = 'block';
  
  const chineseZodiacContent = document.getElementById('chineseZodiacContent');
  if (chineseZodiacContent) {
    chineseZodiacContent.innerHTML = `
      <div class="flex items-center mb-4">
        <img src="${data.image}" alt="${data.name}" class="w-16 h-16 mr-4">
        <div>
          <h3 class="text-xl font-bold">${data.name}</h3>
          <p class="text-gray-600">${data.years}</p>
        </div>
      </div>
      <div class="space-y-3">
        <div>
          <h4 class="font-medium">性格特点</h4>
          <p>${data.personality}</p>
        </div>
        <div>
          <h4 class="font-medium">五行属性</h4>
          <p>${data.fiveElements}</p>
        </div>
        <div>
          <h4 class="font-medium">吉祥物</h4>
          <p>${data.mascot}</p>
        </div>
        <div>
          <h4 class="font-medium">幸运数字</h4>
          <p>${data.luckyNumbers}</p>
        </div>
        <div>
          <h4 class="font-medium">幸运颜色</h4>
          <p>${data.luckyColors}</p>
        </div>
        <div>
          <h4 class="font-medium">最佳配对</h4>
          <p>${data.bestMatch}</p>
        </div>
      </div>
    `;
  }
}

// 姓名测算功能
function initNameAnalysisTool() {
  const nameAnalysisForm = document.getElementById('nameAnalysisForm');
  if (!nameAnalysisForm) return;
  
  nameAnalysisForm.addEventListener('submit', function(e) {
    e.preventDefault();
    
    const name = document.getElementById('nameInput').value;
    
    // 姓名测算
    toolAPI.nameAnalysis(name)
      .then(response => {
        if (response.code === 200) {
          showNameAnalysisResult(response.data);
        } else {
          showToast(response.message || '姓名测算失败');
        }
      })
      .catch(error => {
        showToast(error.message || '姓名测算失败');
      });
  });
}

// 显示姓名测算结果
function showNameAnalysisResult(data) {
  const resultContainer = document.getElementById('nameAnalysisResultContainer');
  if (!resultContainer) return;
  
  resultContainer.style.display = 'block';
  
  const nameAnalysisContent = document.getElementById('nameAnalysisContent');
  if (nameAnalysisContent) {
    nameAnalysisContent.innerHTML = `
      <h3 class="text-xl font-bold mb-4">${data.name} 姓名分析</h3>
      <div class="space-y-4">
        <div>
          <h4 class="text-lg font-medium text-indigo-700">五行属性</h4>
          <p>${data.fiveElements}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">姓名得分</h4>
          <div class="flex items-center">
            <div class="text-2xl font-bold text-indigo-600">${data.score}</div>
            <div class="ml-2 text-gray-600">/ 100</div>
          </div>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">姓名评价</h4>
          <p>${data.evaluation}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">性格特点</h4>
          <p>${data.personality}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">事业发展</h4>
          <p>${data.career}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">财运分析</h4>
          <p>${data.wealth}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">健康状况</h4>
          <p>${data.health}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">人际关系</h4>
          <p>${data.relationships}</p>
        </div>
      </div>
    `;
  }
}

// 周公解梦功能
function initDreamTool() {
  const dreamForm = document.getElementById('dreamForm');
  if (!dreamForm) return;
  
  dreamForm.addEventListener('submit', function(e) {
    e.preventDefault();
    
    const keyword = document.getElementById('dreamKeyword').value;
    
    // 周公解梦
    toolAPI.dreamInterpretation(keyword)
      .then(response => {
        if (response.code === 200) {
          showDreamResult(response.data);
        } else {
          showToast(response.message || '解梦失败');
        }
      })
      .catch(error => {
        showToast(error.message || '解梦失败');
      });
  });
}

// 显示解梦结果
function showDreamResult(data) {
  const resultContainer = document.getElementById('dreamResultContainer');
  if (!resultContainer) return;
  
  resultContainer.style.display = 'block';
  
  const dreamContent = document.getElementById('dreamContent');
  if (dreamContent) {
    dreamContent.innerHTML = `
      <h3 class="text-xl font-bold mb-4">关于"${data.keyword}"的解梦</h3>
      <div class="space-y-4">
        <div>
          <h4 class="text-lg font-medium text-indigo-700">梦境解析</h4>
          <p>${data.interpretation}</p>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">吉凶指数</h4>
          <div class="flex items-center">
            <div class="text-yellow-500">${'★'.repeat(data.luckyIndex)}</div>
            <div class="text-gray-300">${'★'.repeat(5 - data.luckyIndex)}</div>
            <div class="ml-2 text-gray-600">${data.luckyIndex >= 3 ? '吉' : '凶'}</div>
          </div>
        </div>
        <div>
          <h4 class="text-lg font-medium text-indigo-700">提示信息</h4>
          <p>${data.tips}</p>
        </div>
      </div>
    `;
  }
}

// 页面加载完成后初始化
document.addEventListener('DOMContentLoaded', function() {
  initToolsModule();
});
